/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 */

package com.nail.zhaogr.front.action;

import java.util.List;

import org.apache.taglibs.standard.tag.common.core.ForEachSupport;

import javacommon.util.iplocation.Util;
import cn.org.rapid_framework.page.Page;

import com.nail.zhaogr.model.Category;
import com.nail.zhaogr.model.City;
import com.nail.zhaogr.model.Members;
import com.nail.zhaogr.model.MyContact;
import com.nail.zhaogr.model.MyFavorite;
import com.nail.zhaogr.model.PublishInfo;
import com.nail.zhaogr.service.CategoryManager;
import com.nail.zhaogr.service.CityManager;
import com.nail.zhaogr.service.CommentsManager;
import com.nail.zhaogr.service.MyContactManager;
import com.nail.zhaogr.service.MyFavoriteManager;
import com.nail.zhaogr.service.PublishInfoManager;
import com.nail.zhaogr.vo.query.CommentsQuery;
import com.nail.zhaogr.vo.query.MyContactQuery;
import com.nail.zhaogr.vo.query.MyFavoriteQuery;
import com.nail.zhaogr.vo.query.PublishInfoQuery;

/**
 * @author badqiu email:badqiu(a)gmail.com
 * @version 1.0
 * @since 1.0
 */


public class MemberCenterAction extends BaseAction{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	//forward paths
	protected static final String Online_JSP= "/front/Members/onlineMsg.jsp";
	
	protected static final String Audit_JSP= "/front/Members/auditMsg.jsp";
	
	protected static final String Close_JSP= "/front/Members/closedMsg.jsp";
	
	protected static final String Deleted_JSP= "/front/Members/deletedMsg.jsp";
	
	protected static final String Member_JSP = "/front/Members/memberIndex.jsp";
	
	protected static final String Corp_Member_JSP = "/front/CorpMembers/corpMemberIndex.jsp";
	
	protected static final String MY_PUBLISH = "/front/Members/myPublish.jsp";
	
	protected static final String COMMENT_BY_ME = "/front/Members/commentByMe.jsp";
	
	protected static final String MY_FAVORITE = "/front/Members/myFavorite.jsp";
	
	protected static final String CONTACTED_LIST = "/front/Members/contactedList.jsp";
	
	private PublishInfoManager publishInfoManager;
	
	private CityManager cityManager;
	private CategoryManager categoryManager;
	
	private MyFavoriteManager myFavoriteManager;
	
	private MyContactManager myContactManager;
	

	private PublishInfo publishInfo;
	java.lang.Long id = null;
	private String[] items;
	
	private List<City> cityList;
	private List<Category> categoryList;
	
	private List<PublishInfo> publishInfoList;

	private CommentsManager commentsManager;
	
	public CommentsManager getCommentsManager() {
		return commentsManager;
	}
	
	public MyFavoriteManager getMyFavoriteManager() {
		return myFavoriteManager;
	}

	public void setMyFavoriteManager(MyFavoriteManager myFavoriteManager) {
		this.myFavoriteManager = myFavoriteManager;
	}
	public void setCommentsManager(CommentsManager commentsManager) {
		this.commentsManager = commentsManager;
	}
	
	public MyContactManager getMyContactManager() {
		return myContactManager;
	}

	public void setMyContactManager(MyContactManager myContactManager) {
		this.myContactManager = myContactManager;
	}
	
	public void prepare() throws Exception {
		
		this.getPageUrl();
		if(getMemberInfo()!=null)
		{
			if (isNullOrEmptyString(id)) {
				publishInfo = new PublishInfo();
			} else {
				publishInfo = (PublishInfo)publishInfoManager.getById(id);
			}
		}else{
			this.addActionError("请先注册再发布信息");
			this.getResponse().sendRedirect("/front/PublishInfo/create.jsp");
		}
		
	}
	
	/** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性 */
	public void setPublishInfoManager(PublishInfoManager manager) {
		this.publishInfoManager = manager;
	}	
	
	public Object getModel() {
		return publishInfo;
	}
	
	public void setPublishInfoId(java.lang.Long val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}
	public String index(){
		Members members =getMemberInfo();
		if(members.getType()==1){
			return Member_JSP;
		}else{
			return Corp_Member_JSP;
		}
		
	}
	//显示中的信息
	public String getOnlineMsg(){
		PublishInfoQuery query = newQuery(PublishInfoQuery.class,DEFAULT_SORT_COLUMNS);
		query.setStatus(1);
		query.setMemberId(getMemberInfo().getMemberId());
		Page page = publishInfoManager.findPage(query);
		savePage(page,query);
		return Online_JSP;
	}
	//审核中的信息
	public String getAuditingMsg(){
		PublishInfoQuery query = newQuery(PublishInfoQuery.class,DEFAULT_SORT_COLUMNS);
		query.setStatus(0);
		query.setMemberId(getMemberInfo().getMemberId());
		Page page = publishInfoManager.findPage(query);
		savePage(page,query);
		return Audit_JSP;
	}
	//关闭的信息
	public String getClosedMsg(){
		PublishInfoQuery query = newQuery(PublishInfoQuery.class,DEFAULT_SORT_COLUMNS);
		query.setStatus(3);
		query.setMemberId(getMemberInfo().getMemberId());
		Page page = publishInfoManager.findPage(query);
		savePage(page,query);
		return Close_JSP;
	}
	//删除的信息，审核不通过
	public String getDeletedMsg(){
		PublishInfoQuery query = newQuery(PublishInfoQuery.class,DEFAULT_SORT_COLUMNS);
		query.setStatus(2);
		query.setMemberId(getMemberInfo().getMemberId());
		Page page = publishInfoManager.findPage(query);
		savePage(page,query);
		return Deleted_JSP;
	}
	
	// Search my publishment
	public String myPublish() {
		Members members = this.getMemberInfo();
		if (members != null) {
			final PublishInfoQuery query = newQuery(PublishInfoQuery.class,
					DEFAULT_SORT_COLUMNS);
			query.setMemberId(members.getMemberId());
			Page page = publishInfoManager.findPage(query);
			limitLength(page);
			savePage(page, query);
		}
		return MY_PUBLISH;
	}
	
	/** 执行搜索 */
	public String commentByMe() {
		Members members = this.getMemberInfo();
		if(members!=null)
		{
			CommentsQuery query = newQuery(CommentsQuery.class,DEFAULT_SORT_COLUMNS);
			query.setMemberId(members.getMemberId());
			Page page = commentsManager.findPage(query);
			savePage(page,query);
		}
		return COMMENT_BY_ME;
	}
	
	/** search people who i contacted 执行搜索 */
	public String contactedList() {
		Members members = this.getMemberInfo();
		if(members!=null)
		{
			MyContactQuery query = newQuery(MyContactQuery.class,DEFAULT_SORT_COLUMNS);
			query.setMemberId(members.getMemberId());
			Page page = myContactManager.findPage(query);
			savePage(page,query);
		}
		return CONTACTED_LIST;
	}
	
	/** Search my favorite  */
	public String myFavorite() {
		Members member = this.getMemberInfo();
		if(member!=null)
		{
			MyFavoriteQuery query = newQuery(MyFavoriteQuery.class,DEFAULT_SORT_COLUMNS);
			query.setMemberId(member.getMemberId());
			Page page = myFavoriteManager.findPage(query);
			savePage(page,query);
		}
		return MY_FAVORITE;
	}
	
	public String removeFavorite() {
		Members member = this.getMemberInfo();
		if(member!=null)
		{
			if(publishInfo.getId() != null)
			{
				Long id = Long.parseLong(publishInfo.getId());
				myFavoriteManager.removeById(id);
			}
			myFavorite();
		}
		return MY_FAVORITE;
	}
	
	/** add my favor*/
	public String addMyFavorite() {
		Members member = this.getMemberInfo();
		if(member!=null && Util.nonBlankOrNull(publishInfo.getId()))
		{
			Long publishId = Long.parseLong(publishInfo.getId());
			MyFavorite mf = new MyFavorite();
			mf.setMemberId(member.getMemberId());
			mf.setPublishInfoId(publishId);
			myFavoriteManager.saveOrUpdate(mf);
			myFavorite();
		}
		return MY_FAVORITE;
	}
	
	//我评价的信息
	/*public String getCommentMsg(){
		PageRequest<Map> pageRequest = newPageRequest(DEFAULT_SORT_COLUMNS);
		pageRequest.getFilters().put("status",2);
		page = publishInfoManager.findByPageRequest(pageRequest);
		savePage(page,pageRequest);
		return LIST_JSP;
	}*/
	//信用明细
	/*public String getCreditInfo(){
		PageRequest<Map> pageRequest = newPageRequest(DEFAULT_SORT_COLUMNS);
		//pageRequest.getFilters().put("status",2);
		pageRequest.getFilters().put("memberId",getMemberInfo().getMemberId());
		page = publishInfoManager.findByPageRequest(pageRequest);
		savePage(page,pageRequest);
		return LIST_JSP;
	}*/
	
	public List<City> getCityList() {
		return cityList;
	}

	public void setCityList(List<City> cityList) {
		this.cityList = cityList;
	}

	public List<Category> getCategoryList() {
		return categoryList;
	}

	public void setCategoryList(List<Category> categoryList) {
		this.categoryList = categoryList;
	}


	public void setCityManager(CityManager cityManager) {
		this.cityManager = cityManager;
	}

	public void setCategoryManager(CategoryManager categoryManager) {
		this.categoryManager = categoryManager;
	}

	public List<PublishInfo> getPublishInfoList() {
		return publishInfoList;
	}

	public void setPublishInfoList(List<PublishInfo> publishInfoList) {
		this.publishInfoList = publishInfoList;
	}

}
